package com.ylw.rocketMq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @Description
 * @Author away
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2019/9/7
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        try {
            //Instantiate with a producer group name.
            DefaultMQProducer producer = new
                    DefaultMQProducer("please_rename_unique_group_name");
            // Specify name server addresses.
//            producer.setNamesrvAddr("away-aliyun:9876");
            producer.setNamesrvAddr("47.99.209.115:9876");
//            producer.setNamesrvAddr("localhost:9876");
//            producer.setNamesrvAddr("yangliweideMacBook-Pro.local:9876");
            producer.setVipChannelEnabled(false);

            //Launch the instance.
            producer.start();
            for (int i = 0; i < 100; i++) {
                //Create a message instance, specifying topic, tag and message body.
                Message msg = new Message("awayTest" /* Topic */,
//                        "TagA" /* Tag */,
                        ("Hello RocketMQ " +
                                i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
                );
                //Call send message to deliver message to one of brokers.
                SendResult sendResult = producer.send(msg,6000);
                System.out.printf("%s%n", sendResult);
            }
            //Shut down once the producer instance is not longer in use.
            producer.shutdown();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


//    SendResult [sendStatus=SEND_OK, msgId=C0A86467718218B4AAC2275EFB010000, offsetMsgId=2F63D17300002A9F0000000000000000, messageQueue=MessageQueue [topic=awayTest, brokerName=broker-a, queueId=3], queueOffset=0]

//    ConsumeMessageThread_1 Receive New Messages: [MessageExt [queueId=3, storeSize=167, queueOffset=0, sysFlag=0, bornTimestamp=1567927736067, bornHost=/112.10.34.73:17920, storeTimestamp=1567927736102, storeHost=/47.99.209.115:10911, msgId=2F63D17300002A9F0000000000000000, commitLogOffset=0, bodyCRC=613185359, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='awayTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=1, CONSUME_START_TIME=1567928860998, UNIQ_KEY=C0A86467718218B4AAC2275EFB010000, WAIT=true}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 48], transactionId='null'}]]
}